package com.labManage.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@Controller
public class VerificationCodeController {
    /***************************************************************************************************************************
     *  @fn             create
     *  @brief          controller方法
     *  @type           非静态方法
     *  @detail         当请求访问/verificationCode.do时交给与之对应的controller方法
     *  @param[in]      HttpServletRequest request     将验证码数据放在session对象中，在登录的controller方法中将其取出来与前端接收的验证码
     *                                                 做匹配
     *  @param[in]      HttpServletResponse response   将生成的验证码图片返回到页面上
     *  @return         String                         3返回生成的验证码字符串
     *  @author         杜健华，杜志华
     *  @date           2021.06.18
     ****************************************************************************************************************************/
    @RequestMapping("/verificationCode")
    @ResponseBody
    public static String create(HttpServletRequest request,HttpServletResponse response) {
        int width = 100;
        int height = 40;
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphic = image.getGraphics();
        graphic.setColor(Color.getColor("#55b7a4"));
        graphic.fillRect(0, 0, width, height);
        Color[] colors = new Color[] { Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.BLACK, Color.ORANGE,
                Color.CYAN };
//        // 在 "画板"上生成干扰线条 ( 50 是线条个数)
//        for (int i = 0; i < 50; i++) {
//            graphic.setColor(colors[random.nextInt(colors.length)]);
//            final int x = random.nextInt(width);
//            final int y = random.nextInt(height);
//            final int w = random.nextInt(20);
//            final int h = random.nextInt(20);
//            final int signA = random.nextBoolean() ? 1 : -1;
//            final int signB = random.nextBoolean() ? 1 : -1;
//            graphic.drawLine(x, y, x + w * signA, y + h * signB);
//        }

        String verificationCode = "";
        // 在 "画板"上绘制字母
        graphic.setFont(new Font("STYLE_PLAIN", Font.BOLD, 30));
        for (int i = 0; i < 4; i++) {
            final int temp = random.nextInt(26) + 97;
            String s = String.valueOf((char) temp);
            sb.append(s);
            verificationCode = verificationCode + s;
            graphic.setColor(colors[random.nextInt(colors.length)]);
            graphic.drawString(s, i * (width / 4), height - (height / 3));
        }
        HttpSession session = request.getSession();
        session.setAttribute("verificationCode",verificationCode);
        graphic.dispose();
        try {
            ImageIO.write(image, "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
